#include "ipchangedialog.h"

IPChangeDialog::IPChangeDialog(QWidget * parent):
    QDialog(parent)
{
    setWindowTitle("Change IP address");
    mainLayout = new QHBoxLayout(this);
    ipLayout = new QGridLayout();
    toolsLayout = new QVBoxLayout();
    ipLineEdit = new QLineEdit(this);
    maskLineEdit = new QLineEdit(this);
    ipLabel = new QLabel("IP Address:",this);
    maskLabel = new QLabel("Subnet mask:",this);
    okButton = new QPushButton("OK",this);
    okButton->setEnabled(false);
    cancleButton = new QPushButton("Cancel",this);

    ipLayout->addWidget(ipLabel,1,1);
    ipLayout->addWidget(ipLineEdit,1,2);
    ipLayout->addWidget(maskLabel,2,1);
    ipLayout->addWidget(maskLineEdit,2,2);

    toolsLayout->addWidget(okButton);
    toolsLayout->addWidget(cancleButton);
    toolsLayout->addStretch();

    mainLayout->addLayout(ipLayout);
    mainLayout->addLayout(toolsLayout);

    QRegExp ipRegExp("^((\\d{1,2})|([01]\\d{2,2})|(2[0-5]{2,2}))\\.((\\d{1,2})|([01]\\d{2,2})|(2[0-5]{2,2}))\\.((\\d{1,2})|([01]\\d{2,2})|(2[0-5]{2,2}))\\.((\\d{1,2})|([01]\\d{2,2})|(2[0-5]{2,2}))");
    QRegExpValidator* ipValidator = new QRegExpValidator(ipRegExp,ipLineEdit);
    ipLineEdit->setValidator(ipValidator);

    QRegExp maskRegExp("(0|128|192|224|240|248|252|254|255)\\.(0|128|192|224|240|248|252|254|255)\\.(0|128|192|224|240|248|252|254|255)\\.(0|128|192|224|240|248|252|254|255)");
    QRegExpValidator* maskValidator = new QRegExpValidator(maskRegExp,maskLineEdit);
    maskLineEdit->setValidator(maskValidator);

    setMaximumSize(sizeHint());
    setMinimumSize(sizeHint());

    QObject::connect(ipLineEdit,SIGNAL(textChanged(QString)),this,SLOT(ipLineEditTextChanged()));
    QObject::connect(okButton,SIGNAL(clicked()),this,SLOT(accept()));
    QObject::connect(cancleButton,SIGNAL(clicked()),this,SLOT(reject()));
}

void IPChangeDialog::ipLineEditTextChanged(){
    okButton->setEnabled(ipLineEdit->hasAcceptableInput());
}
